package com.util;


import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class IPUtil {





	public static String getIpAddr(HttpServletRequest request) {

		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("http_client_ip");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		// 如果是多级代理，那么取第一个ip为客户ip
		if (ip != null && ip.indexOf(",") != -1) {
			ip = ip.substring(ip.lastIndexOf(",") + 1, ip.length()).trim();
		}
		return ip;


}
	public static List<String> getBetweenTime(String starttime, String endtime)
	{
		List<String> betweenTime = new ArrayList<String>();
		try
		{
			Date sdate= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(starttime);
			Date edate= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endtime);

			SimpleDateFormat outformat = new SimpleDateFormat("yyyy-MM-dd");

			Calendar sCalendar = Calendar.getInstance();
			sCalendar.setTime(sdate);
			int year = sCalendar.get(Calendar.YEAR);
			int month = sCalendar.get(Calendar.MONTH);
			int day = sCalendar.get(Calendar.DATE);
			sCalendar.set(year, month, day, 0, 0, 0);

			Calendar eCalendar = Calendar.getInstance();
			eCalendar.setTime(edate);
			year = eCalendar.get(Calendar.YEAR);
			month = eCalendar.get(Calendar.MONTH);
			day = eCalendar.get(Calendar.DATE);
			eCalendar.set(year, month, day, 0, 0, 0);

			while (sCalendar.before(eCalendar))
			{
				betweenTime.add(outformat.format(sCalendar.getTime()));
				sCalendar.add(Calendar.DAY_OF_YEAR, 1);
			}
			betweenTime.add(outformat.format(eCalendar.getTime()));
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return betweenTime;
	}

	/**
	 * 获取文件内容
	 * @param files    文件路径
	 * @return
	 * @throws IOException
	 */
	public static String getguo_nei_wai(String files) throws IOException
	{
		StringBuilder result = new StringBuilder();
		File file = new File(files);
		BufferedReader br = new BufferedReader(new FileReader(file));  //构造一个BufferedReader类来读取文件
		String data = "";
		while((data = br.readLine()) != null) {    //使用readLine方法，一次读一行
			 result.append(System.lineSeparator()+data);
		}
		br.close();
		if(result != null){
			return result.toString();
		}
		return data;
	}

	/**
	 * 写入文件内容
	 * @param files     文件路径
	 * @param context    文件内容
	 * @return
	 * @throws IOException
	 */
	public static String setguo_nei_wai(String files,String context) throws IOException
	{
		FileOutputStream fos = new FileOutputStream(files);
		fos.write(context.getBytes());
		fos.close();
		return "写入成功";
	}
}
